package com.atguigu.ucenter.service.impl;

import com.atguigu.commonUtils.JwtUtils;
import com.atguigu.commonUtils.MD5;
import com.atguigu.commonUtils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.ucenter.entity.Member;
import com.atguigu.ucenter.entity.vo.RegisterVo;
import com.atguigu.ucenter.mapper.MemberMapper;
import com.atguigu.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Rayyy666
 * @since 2022-04-14
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 用户登录
     *
     * @param member
     * @return
     */
    @Override
    public String login(Member member) {
        String mobile = member.getMobile();
        String password = member.getPassword();

        //校验参数 判断手机号或密码是否为空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登录失败");
        }

        //获取会员
        Member user = baseMapper.selectOne(new QueryWrapper<Member>().eq("mobile", mobile));
        if (user == null) {
            throw new GuliException(20001, "手机号或密码错误！");
        }
        //判断密码
        //因为存储到数据库中的密码是加密过的，把我们输入的密码进行加密再和数据库进行比较
        if (!MD5.encrypt(password).equals(user.getPassword())) {
            throw new GuliException(20001, "手机号或密码错误！");
        }
        //用户是否禁用
        if (user.getDisabled()) {
            throw new GuliException(20001, "登录失败");
        }

        //登录成功，生成token字符串，使用jwt工具类

        String jwtToken = JwtUtils.getJwtToken(user.getId(), user.getNickname());

        return jwtToken;
    }

    /**
     * 用户注册
     *
     * @param registerVo
     * @return
     */
    @Override
    public R register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//手机号
        String password = registerVo.getPassword();//密码
        String nickName = registerVo.getNickName();//昵称

        //非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(code) || StringUtils.isEmpty(password) || StringUtils.isEmpty(nickName)) {
            throw new GuliException(20001, "注册失败");
        }

        //判断验证码
        //判断验证码和redis中的是否一样
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!code.equals(redisCode)) {
            throw new GuliException(20001, "注册失败");
        }

        //判断手机号是否重复
        Integer count = baseMapper.selectCount(new QueryWrapper<Member>().eq("mobile", mobile));
        if (count > 0) {

            throw new GuliException(20001, "注册失败");
        }

        //注册成功，数据添加到数据库中
        Member member = new Member();
        member.setMobile(registerVo.getMobile());
        member.setPassword(MD5.encrypt(registerVo.getPassword()));
        member.setNickname(registerVo.getNickName());
        member.setDisabled(false);
        member.setAvatar("https://guliedu-rayyy.oss-cn-beijing.aliyuncs.com/2022/04/06/default.jpg");
        baseMapper.insert(member);

        return R.ok().message("注册成功！");
    }

    /**
     * 查询微信扫码人信息
     *
     * @param openid
     * @return
     */
    @Override
    public Member getOpenIdMember(String openid) {

        Member member = baseMapper.selectOne(new QueryWrapper<Member>().eq("openid", openid));
        return member;
    }

    /**
     * 查询某一天的注册人数
     *
     * @param day
     * @return
     */
    @Override
    public Integer countRegisterOneDay(String day) {

        return baseMapper.countRegisterOneDay(day);
    }
}
